<?php
namespace app\admin\controller; 
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
class Coupon extends Base {
	
	public function couponlist(){
		return $this->fetch();
	}
	
	public function couponajax(){
		
		$endpage = config('PageSize');
		$orderby1 = input('orderby1');
		$orderby2 = input('orderby2');		
		$storeid = session('store_id');
		
        $where = " where ml_coupon.storeid= $storeid "; // 搜索条件    
		
		$key_word = I('key_word') ? trim(I('key_word')) : '';
		
        if($key_word)
        {
            $where .= " and cname like '%$key_word%'" ;
        }
		
		$count = Db::query("select count(id) from ml_coupon $where")[0]['count(id)'];//M('coupon')->where($where)->count();
        $Page  = new AjaxPage($count,$endpage);
		
		$result = Db::query("select * from ml_coupon $where order by $orderby1 $orderby2 limit ?,?",[$Page->firstRow,$Page->listRows]);
		
		$show = $Page->show();		
		$this->assign('clist',$result);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
	}
	
	public function couponinfo(){
		$cid = I('id/d',0);
        $type = $cid > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        $storeid = session('store_id');	
        //ajax提交验证
        if (IS_POST) {
            // 数据验证
            $data = input('post.');
            $validate = \think\Loader::validate('CouPonEdit');
            if (!$validate->batch()->check($data)) {
                $error = $validate->getError();
                
                $error_msg = array_values($error);
                $return_arr = array(
                    'status' => -1,
                    'msg' => $error_msg[0],
                    'data' => $error,
                );
                $this->ajaxReturn($return_arr);
            }
			
			$data['starttime'] = $data['starttime']*24*60*60;
			$data['endtime'] = $data['endtime']*24*60*60;
            $cname = $data['cname'];
            if ($type == 2) {				
            	M('Coupon')->where("id=$cid")->save($data);
            } else {
            	$data['addtime'] = time();	
				$data['storeid'] = $storeid;				
				M('Coupon')->add($data);
                $cid = M('beautician')->getLastInsID();
            }
			
            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('Admin/Coupon/couponlist')),
            );
            $this->ajaxReturn($return_arr);
        }

        $cInfo = Db::name('Coupon')->where('id=' . I('GET.id', 0))->find();
        $this->assign('info', $cInfo);
		return $this->fetch();
	}
	
	//删除优惠券
	public function delcoupon(){
		$ids = I('post.ids','');
        empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        $c_ids = rtrim($ids,",");
		
		$gcount = M("coupontogoods")->whereIn('couponid',$c_ids)->count();
		if($gcount>0){
			$this->ajaxReturn(['status' => -1,'msg' => '部分优惠券已经绑定对应商品']);
		}
		
        // 删除此优惠券      
        M("coupon")->whereIn('id',$c_ids)->delete();  //

        $this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>U("Admin/Coupon/couponlist")]);
	}
	
	//优惠券列表
	function search_coupon(){
		$goodsid = input('goods_id/d',0);
		
		$endpage = config('PageSize');	
		$storeid = session('store_id');
		
        $where = " where ml_coupon.storeid= $storeid "; // 搜索条件    
		
		$key_word = I('key_word') ? trim(I('key_word')) : '';
		
        if($key_word)
        {
            $where .= " and cname like '%$key_word%'" ;
        }
		
		$count = Db::query("select count(id) from ml_coupon $where")[0]['count(id)'];//M('coupon')->where($where)->count();
        $Page  = new AjaxPage($count,$endpage);		
		$result = Db::query("select * from ml_coupon $where order by id desc limit ?,?",[$Page->firstRow,$Page->listRows]);
		
		$goodscoupon = Db::name('coupontogoods')->where(['goodsid'=>$goodsid])->select();
		
		foreach($result as $key=>$value){
			$result[$key]['ischeck']= 0;
			foreach($goodscoupon as $gkey=>$gvalue){
				if($value['id'] == $gvalue['couponid']){
					$result[$key]['ischeck'] = 1;
					break;
				}
			}
		}
		
//		exit('$goodsid='.$goodsid.'....'.'goods='.json_encode($result));
		
		$show = $Page->show();		
		$this->assign('clist',$result);	
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
	}
		
}

?>